home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / GDIDIB.PAK / FILEDLG.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  14KB  |  428 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993 - 1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: filedlg.c
  9. //
  10. //  PURPOSE: Shows basic use of "Open" and "Save As" common dialogs.
  11. //
  12. //  FUNCTIONS:
  13. //    CmdOpen - Call the open common dialog and show its results.
  14. //    CmdSave - Calls ProcessSave to save current file.
  15. //    CmdSaveAs - Calls ProcessSave to save current file, optionally with new name.
  16. //    SavingDlgProc - Modeless dialog box which is displayed while saving a file.
  17. //    CmdClose - Close the file unless user decides to cancel.
  18. //    QuerySaveChanges - Query the user if bitmap has changed. 
  19. //
  20. //  COMMENTS:
  21. //
  22. //
  23. //
  24. //  SPECIAL INSTRUCTIONS: N/A
  25. //
  26.  
  27. #include <windows.h>            // required for all Windows applications
  28. #include <windowsx.h>  
  29. #include <commctrl.h>           // prototypes and structure for the common controls.
  30. #include <commdlg.h>            // For common dialogs   
  31.  
  32. #include "globals.h"            // prototypes specific to this application 
  33. #include "resource.h"
  34. #include "statbar.h"
  35. #include "toolbar.h"
  36. #include "palette.h"
  37. #include "dibutil.h"
  38.         
  39. BOOL CALLBACK SavingDlgProc(HWND, UINT, WPARAM, LPARAM);
  40.  
  41. // constant for CmdSave
  42. #define MAXFILENAMELEN 144
  43.  
  44. // buffer for string resources
  45. char szBuffer[50];              // watch out for recursive use of this buffer!
  46.  
  47. //
  48. //  FUNCTION: CmdOpen(HWND, WORD, WORD, HWND)
  49. //
  50. //  PURPOSE: Call the open common dialog and show its results.
  51. //
  52. //  PARAMETERS:
  53. //    hwnd     - The window handle.
  54. //    wCommand - IDM_FILEOPEN
  55. //    wNotify  - Notification number (unused)
  56. //    hwndCtrl - NULL (Unused)
  57. //
  58. //  RETURN VALUE:
  59. //    Always returns 0 - command handled.
  60. //
  61. //  COMMENTS:
  62. //    Assumes there is a resource string describing this command with the
  63. //    same ID as the command ID.  Loads the string and calls UpdateStatusBar
  64. //    to put the string into main pane of the status bar.
  65. //
  66.  
  67. #pragma argsused
  68. LRESULT CmdOpen(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  69. {
  70.     OPENFILENAME ofn = {0}; // common dialog box structure   
  71.     char szFile[256];       // filename string
  72.     char szFileTitle[256];  // file-title string
  73.     char szFilter[256];     // filter string
  74.     char chReplace;         // separator for szFilter
  75.      int i, cbString;        // integer count variables
  76.      int  cbWritten;
  77.      int nResult;
  78.  
  79.      // prompt user if there is an existing bitmap with changes
  80.      nResult = QuerySaveChanges(hwnd);
  81.      if (nResult == IDCANCEL)
  82.           goto ErrExit;
  83.  
  84.      cbWritten = LoadString(hInst, wCommand, szBuffer, sizeof(szBuffer));
  85.      if(cbWritten == 0)
  86.           lstrcpy(szBuffer, "Unknown Command");
  87.     UpdateStatusBar(szBuffer, 0, 0);                  
  88.    
  89.     // Place the terminating null character in the szFile.
  90.     szFile[0] = '\0';
  91.  
  92.     // Load the filter string from the resource file.
  93.     cbString = LoadString(hInst, IDS_FILTERSTRING, szFilter, sizeof(szFilter));
  94.  
  95.     // Add a terminating null character to the filter string.
  96.     chReplace = szFilter[cbString - 1];
  97.     for (i = 0; szFilter[i] != '\0'; i++)
  98.     {
  99.         if (szFilter[i] == chReplace)
  100.             szFilter[i] = '\0';
  101.     }
  102.  
  103.     // Set the members of the OPENFILENAME structure.
  104.     ofn.lStructSize     = sizeof(OPENFILENAME);
  105.     ofn.hwndOwner       = hwnd;
  106.     ofn.lpstrFilter     = szFilter;
  107.     ofn.nFilterIndex    = 1;
  108.     ofn.lpstrFile       = szFile;
  109.     ofn.nMaxFile        = sizeof(szFile);
  110.     ofn.lpstrFileTitle  = szFileTitle;
  111.     ofn.nMaxFileTitle   = sizeof(szFileTitle);
  112.     ofn.lpstrInitialDir = NULL;
  113.     ofn.Flags           = OFN_SHOWHELP | OFN_PATHMUSTEXIST |
  114.                           OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  115.  
  116.     // Display the Open dialog box.
  117.     if (GetOpenFileName(&ofn))
  118.     {
  119.         // successfully obtained filename so open the file
  120.         hBitmap = LoadDIBSection(hWndClient, ofn.lpstrFile);
  121.         
  122.         if (!hBitmap)        
  123.             goto ErrExit;           
  124.        
  125.         SizeClientWindow(hwnd);
  126.           
  127.         // display the bitmap
  128.         InvalidateRect(hWndClient, NULL, TRUE);  
  129.  
  130.         SetWindowTitle(hwnd, ofn.lpstrFile);      
  131.     } 
  132.             
  133. ErrExit:            
  134.     // set the status bar text to original text     
  135.     UpdateStatusBar(SZDESCRIPTION, 0, 0);
  136.     
  137.     return 0;
  138. }
  139.  
  140.  
  141. //  FUNCTION: CmdSave(HWND, WORD, WORD, HWND)
  142. //
  143. //  PURPOSE: Performs file save or save as operations.
  144. //
  145. //  PARAMETERS:
  146. //    hwnd     - The window.
  147. //    wCommand - IDM_FILESAVE or IDM_FILESAVEAS
  148. //    wNotify  - Notification number (unused)
  149. //    hwndCtrl - NULL (unused)
  150. //
  151. //  RETURN VALUE:
  152. //    Always returns 0 - command handled.
  153. //
  154. //  COMMENTS:
  155. //    Assumes there is a resource string describing this command with the
  156. //    same ID as the command ID.  Loads the string and calls UpdateStatusBar
  157. //    to put the string into main pane of the status bar.
  158. //
  159.  
  160. #pragma argsused
  161. LRESULT CmdSave(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  162. {
  163.      char    szFileBuf[255];      // Buffer to hold returned file name
  164.     OPENFILENAME ofn = {0};      // structure for GetSaveFileName common dialog
  165.     char    szTitle[50];         // common dialog box title 
  166.     char    szFilter[256];       // edit control filter string
  167.     char    chReplace;           // separator for szFilter
  168.     int i,  cbString;            // integer count variables   
  169.      int     nResult;             // Result of SaveDIB function
  170.      HWND    hModelessDlg;        // modeless dialog displayed while saving file
  171.      int     cbWritten;
  172.  
  173.     cbWritten = LoadString(hInst, wCommand, szBuffer, sizeof(szBuffer)); 
  174.     if(cbWritten == 0)
  175.           lstrcpy(szBuffer, "Unknown Command");
  176.     UpdateStatusBar(szBuffer, 0, 0);
  177.                       
  178.     // make a local copy of the current file name and "Untitled"
  179.     _fstrcpy((LPSTR)szFileBuf, (LPSTR)szCurrentFile);    
  180.     LoadString(hInst, IDS_UNTITLED, szBuffer, sizeof(szBuffer));
  181.  
  182.     if (wCommand == IDM_FILESAVEAS || 
  183.             !_fstrcmp((LPSTR)szFileBuf, (LPSTR)szBuffer))
  184.     { 
  185.         // performing save as or saving an untitled file; use common dialog
  186.         // to get filename, compression and color resolution.       
  187.  
  188.         // Initialize the OPENFILENAME members
  189.    
  190.         LoadString(hInst, IDS_SAVEASTITLE, szTitle, sizeof(szTitle));        
  191.                                  
  192.         // Load the filter string from the resource file.
  193.           cbString = LoadString(hInst, IDS_FILTERSTRING, szFilter, sizeof(szFilter));
  194.  
  195.         // Add a terminating null character to the filter string.
  196.         chReplace = szFilter[cbString - 1];
  197.         for (i = 0; szFilter[i] != '\0'; i++)
  198.         {
  199.                 if (szFilter[i] == chReplace)
  200.                 szFilter[i] = '\0';
  201.         }
  202.                                      
  203.         ofn.lStructSize       = sizeof(OPENFILENAME);
  204.         ofn.hwndOwner         = hwnd;
  205.           ofn.hInstance         = hInst;
  206.         ofn.lpstrFilter       = szFilter;
  207.         ofn.lpstrCustomFilter = NULL;
  208.         ofn.nMaxCustFilter    = 0L;
  209.         ofn.nFilterIndex      = 1L;
  210.   
  211.           if (_fstrcmp(szFileBuf, (LPSTR)szBuffer))
  212.             // bitmap already has a name
  213.             ofn.lpstrFile = szFileBuf;
  214.         else
  215.             // untitled bitmap needs to have a name
  216.             ofn.lpstrFile = (LPSTR)"*.BMP";   
  217.  
  218.         ofn.nMaxFile          = MAXFILENAMELEN;
  219.         ofn.lpstrFileTitle    = NULL;
  220.         ofn.nMaxFileTitle     = 0;
  221.         ofn.lpstrInitialDir   = NULL;
  222.         ofn.lpstrTitle        = szTitle; 
  223.           ofn.Flags             = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |
  224.                                 OFN_HIDEREADONLY;
  225.         ofn.nFileOffset       = 0;
  226.         ofn.nFileExtension    = 0;
  227.         ofn.lpstrDefExt       = "bmp";      
  228.  
  229.           // Call the GetSaveFilename function
  230.         if (GetSaveFileName(&ofn))
  231.             // copy filename specification given by user
  232.             _fstrcpy((LPSTR)szFileBuf, (LPSTR)ofn.lpstrFile);       
  233.         else
  234.             // user cancelled the operation
  235.                 goto ErrExit;
  236.     }
  237.                
  238.     // display modeless dialog while saving to file   
  239.      hModelessDlg = CreateDialogParam(hInst,
  240.                                      (LPSTR)"Saving",
  241.                                                  hwnd,
  242.                                      SavingDlgProc, 
  243.                                      (DWORD)(LPSTR)szFileBuf);                                             
  244.  
  245.     // save the DIB to disk file
  246.     nResult = SaveDIB(hDIBInfo, lpvBits, szFileBuf);
  247.      if (nResult)
  248.     {
  249.         DIBError(nResult);
  250.         goto ErrExitModeless;
  251.     }
  252.   
  253.      // clear global flag for changes
  254.     fChanges = FALSE;  
  255.                             
  256.     // update the title bar information
  257.     SetWindowTitle(hwnd, szFileBuf);
  258.                                     
  259.      // update status of menu and toolbar
  260.     EnableMenuItem(hMenu, IDM_FILESAVE, MF_DISABLED);
  261.     SendMessage(hWndToolbar, TB_ENABLEBUTTON, IDM_FILESAVE, MAKELONG(FALSE, 0));
  262.     
  263. ErrExitModeless:
  264.     DestroyWindow(hModelessDlg);
  265.  
  266. ErrExit:               
  267.     // set the status bar text to original text     
  268.     UpdateStatusBar(SZDESCRIPTION, 0, 0);
  269.  
  270.      return 0;
  271. }
  272.  
  273.  
  274. //
  275. //  FUNCTION: SavingDlgProc(HWND, UINT, WPARAM, LPARAM)
  276. //
  277. //  PURPOSE: This is a modeless dialog box which is called when the bitmap is
  278. //           saved to a file (so the user dosen't think the machine is hung).
  279. //
  280. //  PARAMETERS:
  281. //    hdlg      - The window handle.
  282. //    uMessage  - The message to process
  283. //    wparam    - extra data
  284. //    lparam    - extra data
  285. //
  286. //  RETURN VALUE:
  287. //    TRUE if message was processed.
  288. //    FALSE if the system needs to process the message.
  289. //
  290. //  COMMENTS:
  291. //
  292. //
  293. //
  294. #pragma argsused
  295. BOOL CALLBACK SavingDlgProc(HWND hdlg, UINT uMessage, WPARAM wparam,
  296.     LPARAM lparam)
  297. {
  298.     switch (uMessage)
  299.     {
  300.         case WM_SETFOCUS:
  301.                 MessageBeep(0);
  302.             break;
  303.  
  304.         case WM_INITDIALOG:
  305.             // Center the dialog over the application window
  306.             CenterWindow(hdlg, GetWindow(hdlg, GW_OWNER));
  307.  
  308.             // set the icon of this dialog to the class icon
  309.             SendMessage(hdlg, WM_SETICON, (WPARAM)FALSE, (LPARAM)hIcon);
  310.  
  311.             // Set the text of the filename in the dialog box.  This dialog
  312.             // should be called with DialogBoxParam, and the parameter should
  313.                 // be a pointer to the filename.  It shows up as the lParam here.
  314.             SetDlgItemText(hdlg, IDD_FILETEXT, (LPSTR)lparam);
  315.             return TRUE;
  316.  
  317.           case WM_DESTROY:
  318.                 return TRUE;
  319.  
  320.         default:
  321.             return FALSE;
  322.      }
  323.      return TRUE;
  324. }
  325.  
  326.  
  327. //
  328. //  FUNCTION: CmdClose(HWND, WORD, WORD, HWND)
  329. //
  330. //  PURPOSE: Processes the IDM_FILECLOSE message, queries user if there are 
  331. //           changes
  332. //
  333. //  PARAMETERS:
  334. //    hwnd     - The window handle.
  335. //    wCommand - IDM_FILECLOSE (Unused)
  336. //    wNotify  - Notification number (unused)
  337. //    hwndCtrl - NULL (Unused)
  338. //
  339. //  RETURN VALUE:
  340. //    Always returns 0 - command handled.
  341. //
  342. //  COMMENTS:
  343. //    
  344. //
  345.  
  346. #pragma argsused
  347. LRESULT CmdClose(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  348. {
  349.     int  cbWritten;
  350.     int nResult;
  351.   
  352.     cbWritten = LoadString(hInst, wCommand, szBuffer, sizeof(szBuffer)); 
  353.     if(cbWritten == 0)
  354.         lstrcpy(szBuffer, "Unknown Command");        
  355.     UpdateStatusBar(szBuffer, 0, 0);
  356.     
  357.     // prompt user first in case the existing bitmap has unsaved changes
  358.     nResult = QuerySaveChanges(hwnd);
  359.     if (nResult == IDCANCEL)
  360.         return 0;
  361.    
  362.     // remove the current DIB section and palette
  363.     RemoveDIBSection();
  364.     
  365.     // create default wash for application palette
  366.      if (bPalDevice)
  367.     {
  368.         HDC hdc;
  369.  
  370.         hdc = GetDC(hwnd);
  371.         hPalette = CreateHalftonePalette(hdc);
  372.         ReleaseDC(hwnd, hdc);
  373.     }
  374.                               
  375.     // reset the window title
  376.     SetWindowTitle(hwnd, "");
  377.      
  378.     // update the client window
  379.     SizeClientWindow(hwnd);
  380.                      
  381.     // set the status bar text to original text     
  382.     UpdateStatusBar(SZDESCRIPTION, 0, 0);
  383.     
  384.      return 1;
  385. }
  386.  
  387.  
  388. //
  389. //  FUNCTION: QuerySaveChanges(HWND)
  390. //
  391. //  PURPOSE: Queries user to save changes in bitmap
  392. //
  393. //  PARAMETERS:
  394. //    hwnd      - The window handle.
  395. //
  396. //  RETURN VALUE:
  397. //    IDYES if user elects to save changes
  398. //    IDNO if user elects to abandon changes or there were no changes
  399. //    IDCANCEL if user wants to abandon calling process
  400. //
  401. //  COMMENTS:
  402. //
  403. //
  404. // 
  405. int QuerySaveChanges(HWND hwnd)
  406. {   
  407.     int nResult = IDNO;
  408.     
  409.     if (hBitmap && fChanges)
  410.     {   
  411.         LoadString(hInst, IDS_SAVECHANGES, szBuffer, sizeof(szBuffer));
  412.         
  413.         nResult = MessageBox(hwnd, szBuffer, szAppName, MB_YESNOCANCEL);
  414.         
  415.         if (nResult == IDYES)
  416.             // save the existing bitmap
  417.             CmdSave(hwnd, IDM_FILESAVE, 0, NULL);            
  418.         else if (nResult == IDCANCEL)
  419.             // user has decided to cancel File New
  420.             return IDCANCEL;
  421.             
  422.         // no action needed if IDNO is returned
  423.     }
  424.  
  425.     return nResult;
  426.  
  427.